const {executeQuery} = require("./mongo_util");

const collectionName = "users";

// 注册    userModel.addUser(user,onRetGot,onErrGot)
function addUser(user, onRetGot, onErrGot) {
    executeQuery(collectionName, (collection, callback) => {
        collection
            .insertOne(user)
            .then((result) => {
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}

// 登录    userModel.getUser(user,onRetGot,onErrGot)
function getUser(user, onRetGot, onErrGot) {
    executeQuery(collectionName, (collection, callback) => {
        collection
            .findOne(user)
            .then((ret) => {
                onRetGot(ret);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}

module.exports = {
    addUser,
    getUser,
};
